package com.ziki.account.test;

import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import static org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type.SERVLET;

@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = SERVLET)
@EnableConfigurationProperties()
public class WebMvcConfiguration implements WebMvcConfigurer {
    /**
     * 系统国际化文件配置
     * （1）若找不到带对应语言的后缀的properties文件，默认使用xx.properties中的映射关系
     * （2）若映射关系不存在，会抛出一个异常
     *
     * @return MessageSource
     */
    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        //指定读取国际化配置文件的basename
        messageSource.setBasenames(ResourceUtils.CLASSPATH_URL_PREFIX + "i18n/error", ResourceUtils.CLASSPATH_URL_PREFIX + "i18n/message", ResourceUtils.CLASSPATH_URL_PREFIX + "i18n/warning");
        //指定编码
        messageSource.setDefaultEncoding("UTF-8");
        //指定缓存时间(Second)
        messageSource.setCacheSeconds(3600);
        return messageSource;
    }
}